<?php
include '../../auth/ceklogin.php';
include '../../sql/conn.php';
$text=$_REQUEST['text'];
$jns_inquiry=$_REQUEST['jns_inquiry'];
$to=$_REQUEST['idCustomer'];
$attn=$_REQUEST['attn'];
$telp=$_REQUEST['telp'];
$fax=$_REQUEST['fax'];
$desc=$_REQUEST['desc'];

$queryi=mysql_query("select no_inquiry from inquiry order by id desc limit  0,1");
$roww=mysql_fetch_array($queryi);
$tmp=explode(" ",$roww[0]);

//tahun & bulan
$tmp2=explode("/",$tmp[2]);
$tahun_tmp=$tmp2[3];
$bulan_tmp=$tmp2[2];

//cek bulan & tahun sekarang apakah sudah ganti bulan ato tahun
if($tahun_tmp==date("Y")){
	if($bulan_tmp==date("m")){
		$new=$tmp[1]+1;
	}
	else{
		$new=1;
	}
}
else{
	$new=1;
}

//nomor baru
$len=strlen($new);
for($len;$len<4;$len++){
$new="0".$new;
}

$no = "In ".$new." /".$jns_inquiry."/".date("m")."/".date("Y");

//cek id
$queryu=mysql_query("SELECT id from inquiry order by id desc limit 0,1");
$rowu=mysql_fetch_array($queryu);

$id=$rowu[0]+1;
$query=mysql_query("
    INSERT INTO 
        `inquiry`(`id`,`no_inquiry`,`jns_inquiry`,`text`,`idCustomer`,`attn`,`telp`,`fax`,`desc`,`crtby`,`crtdate`,`status`)
    VALUES 
        ($id, '$no', '$jns_inquiry', '$text', '$to', '$attn', '$telp', '$fax', '$desc', '$_SESSION[userbkknt]', CURRENT_TIMESTAMP, 'OSIN');
");

if($query){
    echo json_encode(array('success'=>true,'msg'=>'Inquiry saved<br>No. Inquiry: '.$no));
} else {
    echo json_encode(array('msg'=>'Error Occured.'.  mysql_error()));
}
?>